package com.nononsenseapps.feeder.mobile.ui.reader.sample

import android.net.Uri
import android.os.Bundle
import android.view.View
import com.gitee.wsl.android.ext.launchAndRepeatWithViewLifecycle
import com.gitee.wsl.android.ext.okhttp.loadUri
import com.gitee.wsl.jvm.okhttp.downloadHttpClient

import com.nononsenseapps.feeder.mobile.R
import com.nononsenseapps.feeder.net.HtmlPageLoader.getReadabilityFullPage
import com.nononsenseapps.feeder.ui.reader.sample.AbstractBaseReaderFragment
import com.nononsenseapps.feeder.util.TabletUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber

class MarkDownViewFragment :  AbstractBaseReaderFragment(){

    var contUri:Uri?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        contUri = arguments?.getParcelable<Uri>(ARG_OPEN_MD_URI)

        Timber.d("init use contUri:$contUri")
        launchAndRepeatWithViewLifecycle {
            Timber.d("launchAndRepeatWithViewLifecycle :$contUri")
            /*contUri?.let {uri->
                requestStoragePermission(RequestAccess.readAllDocumentArg){
                    Timber.d("requestStoragePermission :$it")
                    val title = uri.lastPathSegment?:"readme.md"
                    uri.readContent()?.let {str->
                        updateUi(title,str)
                    }
                }
            }*/
            val title = contUri?.lastPathSegment?:"readme.md"
            updateUi(title, "get content for:$contUri")
            withContext(Dispatchers.IO) {
                downloadHttpClient.loadUri(contUri?:return@withContext) { result ->
                    Timber.d("load cont :$contUri-${result.isSuccess}")
                    launch(Dispatchers.Main) {
                        result.onSuccess { s ->
                            updateUi(title, "success on result,passing content$contUri")
                            if(title.endsWith("md"))
                                updateUi(title, s)
                            else{
                                val readability:String = withContext(Dispatchers.IO){
                                    getReadabilityFullPage(contUri.toString(),s)?:s
                                }
                                updateUi(title,readability)
                            }
                        }.onFailure {
                            updateUi(title, "load error ${it.message}")
                        }
                    }
                }
            }

        }
    }



    override fun initTheme() {
        super.initTheme()
        itemFeatureView.visibility = View.GONE
        itemTagsView.visibility = View.GONE
    }

    override fun getLayoutId(): Int =
        if (TabletUtils.isTablet(activity)) {
            R.layout.fragment_reader_tablet
        } else {
            R.layout.fragment_reader
        }

    override fun navigateToCodeViewFragment() {
        //findNavController().navigate(R.id.action_readerFragment_to_codeViewFragment)
    }

    override fun onOptionsItemSelectedWithFab(id: Int, view: View?) {

    }

    /*override fun navigateToWebViewFragment(urlLink: String) {
        findNavController().navigate(R.id.action_readerFragment_to_readerWebViewFragment, bundle {
            putString(ARG.LINK, urlLink)
        })
    }*/

    companion object{
        val ARG_OPEN_MD_URI = "open_md_uri"
    }

}